//
//  SelectSubViewController.m
//  HTP
//
//  Created by admin on 15/10/31.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "SelectSubViewController.h"
#import "SelectTeacherViewController.h"
#define BUTTONWIDTH (ScreenWidth - 5.0 * 8) / 2.0
#define BUTTONHEIGHT (ScreenWidth - 5.0 * 8) / 4.0
#define BUTTONPADDING 5.0
typedef NS_ENUM(NSInteger , SubjectType) {
    yWenSub       = 0,
    sXueSub       = 1,
    englishSub    = 2,
    kXueSub       = 3,
    wLiSub        = 4,
    hXueSub       = 5 ,
    sWuSub        = 6,
    dLiSub        = 7,
    lShiSub       = 8,
    zZhiSub       = 9
    
};
@interface SelectSubViewController ()
@property (nonatomic,strong) UIScrollView   *mainScrollV;

@property (nonatomic,retain) NSMutableArray *btnArrays;

@property (nonatomic,assign) NSInteger       selectIndex;
@end

@implementation SelectSubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _selectIndex = -1;
    [self.view addSubview:self.mainScrollV];
    [self _initView];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self addCustomTitleWithTitle:@"选择科目"];
    [self addCustomBackWithAction:@selector(backToRoot) normalImg:[UIImage imageNamed:@"back"] selectImg:[UIImage imageNamed:@"back"] withPadding:5];
}
//TODO:重写get方法
-(UIScrollView *)mainScrollV{
    if (!_mainScrollV) {
        _mainScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeihgt - NavHe - 80)];
        _mainScrollV.backgroundColor = [UIColor whiteColor];
        _mainScrollV.userInteractionEnabled = YES;
    }
    return _mainScrollV;
}
-(void)_initView{
    _btnArrays = [NSMutableArray array];
    CGFloat headPadding = BUTTONPADDING * 10;
    CGFloat scrollHeight = BUTTONPADDING * 30 + BUTTONHEIGHT * 5;
    for (int i = 0; i < 10; i++) {
        if (!(i % 2)) {
            UIButton *selectSubBtn = [[UIButton alloc] initWithFrame:CGRectMake(BUTTONPADDING * 3.0, headPadding + (BUTTONPADDING * 4.0 + BUTTONHEIGHT) * (i / 2), BUTTONWIDTH, BUTTONHEIGHT)];
            selectSubBtn.tag = i;
            [selectSubBtn addTarget:self action:@selector(selectSubBtns:) forControlEvents:UIControlEventTouchUpInside];
            [self.mainScrollV addSubview:selectSubBtn];
            [selectSubBtn setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
            [selectSubBtn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"sub_%d",i]] forState:UIControlStateNormal];
            [selectSubBtn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"subs_%d",i]] forState:UIControlStateSelected];
            [_btnArrays addObject:selectSubBtn];
        }else{
            UIButton *selectSubBtn = [[UIButton alloc] initWithFrame:CGRectMake(BUTTONPADDING * 3.0 + BUTTONWIDTH + BUTTONPADDING * 2.0, headPadding + (BUTTONPADDING * 4.0 + BUTTONHEIGHT) * (i / 2), BUTTONWIDTH, BUTTONHEIGHT)];
            selectSubBtn.tag = i;
            [selectSubBtn addTarget:self action:@selector(selectSubBtns:) forControlEvents:UIControlEventTouchUpInside];
            [selectSubBtn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"sub_%d",i]] forState:UIControlStateNormal];
            [selectSubBtn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"subs_%d",i]] forState:UIControlStateSelected];
            [self.mainScrollV addSubview:selectSubBtn];
            [_btnArrays addObject:selectSubBtn];
        }
        
    }
    
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(BUTTONPADDING * 3.0, ScreenHeihgt - 70 - NavHe, ScreenWidth - BUTTONPADDING * 6.0, 50)];
    [sureBtn setBackgroundImage:[UIImage imageNamed:@"ConfirmButton"] forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureSelect) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureBtn];
    
    [self.mainScrollV setContentSize:CGSizeMake(ScreenWidth, scrollHeight)];
}
//TODO:actiom
-(void)backToRoot{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)sureSelect{
    if (_selectIndex == -1) {
        [self showSVProgressFailed:@"请选择科目"];
        return;
    }
    SelectTeacherViewController *selectTeacher = [[SelectTeacherViewController alloc] init];
    selectTeacher.titleIndex = _selectIndex;
    [self.navigationController pushViewController:selectTeacher animated:YES];
}
-(void)selectSubBtns:(id)sender{
    UIButton *selectBtn = (UIButton *)sender;
    NSInteger tag = selectBtn.tag;
    _selectIndex = tag;
    [_btnArrays enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *subBtn = (UIButton *)obj;
        if (subBtn.tag == tag) {
            subBtn.selected = YES;
        }else{
            subBtn.selected = NO;
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
